import http from '../../apis/api.js';
import util from '../../utils/util.js';
const app = getApp();
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagination: {
      page: 1,
      limit: 8,
      more: 0
    },
    loading: false,
    dataList: [],
    custNo: null,
    imageurl: '/images/img1.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.data.dataList = [];
    self.data.pagination={
      page: 1,
      limit: 8,
      more: 0
    };
    if (!self.data.custNo) {
      wx.getStorage({
        key: 'loginInfo',
        success: function (res) {
          if (res.data.id) {
            self.data.custNo = res.data.custNo;
            self.getDataList();
          }
        },
        fail(){
          self.getDataList();
        }
      })
    } else {
      self.getDataList();
    }
  },
  getDataList() {/**获取收藏列表 */  
    let postData = {
      custNo: self.data.custNo,
      limit: self.data.pagination.limit,
      page: self.data.pagination.page
    };
    self.setData({
      loading: true
    })
    http.myVouchersList(postData).then(res=>{
      if (res.code == 0) {
        if (res.page.records.length > 0) {
          res.page.records.forEach((el, i) => {
            if (el.prodEndTime > new Date().getTime()){
              el.statusObj = util.getVouchersStatus(el.useStat)[0];
            }else{
              el.statusObj={
                code: '50300103',
                label: '已过期',
                className: 'dec'
              }
            }
            el.prodStartTimeFormat = util.formatTime(el.prodStartTime,'YYYY.MM.DD');
            el.prodEndTimeFormat = util.formatTime(el.prodEndTime, 'YYYY.MM.DD');
            if (el.prodType =='50400101'){
              el.storePics.forEach(item=>{
                el.thumbnail = item.inPath
              })
              el.briefIntroFormat = util.formateContent(el.briefIntro, 24);
            }else{
              el.thumbnail = self.data.imageurl;
            }
            self.data.dataList.push(el);
          })
          self.data.pagination.more = Number(res.page.current) * Number(res.page.size) < res.page.total ? 1 : 0;
        }
        self.setData({
          dataList: self.data.dataList,
          pagination: self.data.pagination,
          loading: false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (self.data.pagination.more > 0) {
      self.data.pagination.page++;
      self.getDataList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goDetail(e){
    let detail = e.currentTarget.dataset.detail;
    if (detail.prodType == "50400100"){
      if (detail.useStat == "50300101") {
        wx.navigateTo({
          url: `/pages/vouchersUse/vouchersUse?id=${detail.id}`,
        })
      }
    }else{
      wx.navigateTo({
        url: `/pages/vouchersDetail/vouchersDetail?id=${detail.id}`,
      })
    }
  }
})